package com.app.util;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Restrictions;

import com.app.common.City;
import com.app.common.Weather;

public class DBManager {
	public static final String URL_WEATHER_PATTERN = "http://m.weather.com.cn/data/%s.html";

	public boolean isExists(String tableName) {
		boolean isExits = false;
		Session session = new Configuration().configure().buildSessionFactory()
				.openSession();
		@SuppressWarnings("deprecation")
		Connection conn = session.connection();
		DatabaseMetaData metaData;
		ResultSet rs = null;
		try {
			metaData = conn.getMetaData();
			rs = metaData.getTables(null, "test", tableName,
					new String[] { "TABLE" });
			if (rs.next()) {
				isExits = true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return isExits;
	}

	public void initAll() {

	}

	public void initBusInfo() {

	}

	public void initShareInfo() {

	}

	public void initVegInfo() {

	}

	public void initWeatherInfo() {
		List<City> list = getCitysList();
		List<Weather> weatherList = null;
		if (list != null) {
			HttpUtil httpUtil = new HttpUtil();
			JsonParserHelper jsonParserHelper = new JsonParserHelper();
			for (City city : list) {

				String url = String.format(URL_WEATHER_PATTERN,
						city.getCityNum());
				String content = httpUtil.read(url);
				weatherList = jsonParserHelper.parseWather(content, city);
				saveWeather(weatherList);
			}
		}
	}

	public void saveWeather(List<Weather> weatherList) {
		Session session = HibernateSessionFactory.getSession();
		Transaction transaction = session.beginTransaction();
		for (Weather weather : weatherList) {
			session.save(weather);
		}
		transaction.commit();
	}

	public List<City> getCitysList() {
		List<City> list = null;
		Session session = HibernateSessionFactory.getSession();
		Criteria criteria = session.createCriteria(City.class);
		list = criteria.list();
		if (session != null) {
			session.close();
		}
		return list;
	}

	public static void main(String[] args) {
		new DBManager().initWeatherInfo();
	}

	public void updateAll() {

	}

	public void updateBusInfo() {

	}

	public void updateShareInfo() {

	}

	public void updateVegInfo() {

	}

	public void updateWeatherInfo() {

	}

	public List<Weather> loadWeathers(String cityName, Date date, String length) {
		Session session = HibernateSessionFactory.getSession();
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DAY_OF_MONTH, Integer.valueOf(length) - 1);
		Criteria criteria = session.createCriteria(Weather.class);
		criteria.add(Restrictions.between("dateY", date, calendar.getTime()));
		return criteria.list();
	}

	public City getCityByName(String cityName) {
		Session session = HibernateSessionFactory.getSession();
		Query query = session.createQuery("from City c where c.cityName=:name");
		query.setString("name", cityName);
		List<City> cities = query.list();
		return cities.get(0);
	}
}

class weatherComp implements Comparator<Weather> {

	@Override
	public int compare(Weather w1, Weather w2) {

		return w1.getDateY().compareTo(w2.getDateY());
	}

}
